package com.wolfking.datasource;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Objects;
import java.util.logging.Logger;

public class ThreadReuseDataSource implements DataSource {

    private final DataSource delegate;

    public ThreadReuseDataSource(DataSource delegate) {
        this.delegate = delegate;
    }

    @Override
    public Connection getConnection() throws SQLException {
        if (ThreadReuseContextUtils.isReuse()) {
            Connection connection = ThreadReuseContextUtils.reuseConnection();
            if (Objects.nonNull(connection)) {
                ThreadReuseContextUtils.useIncrease();
                return connection;
            } else if (ThreadReuseContextUtils.hasConnection()) {
                return delegate.getConnection();
            } else {
                connection = delegate.getConnection();
                connection = new ThreadReuseConnection(connection);
                ThreadReuseContextUtils.setThreadReuseContext(connection);
                return connection;
            }
        } else {
            return delegate.getConnection();
        }
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        if (ThreadReuseContextUtils.isReuse()) {
            Connection connection = ThreadReuseContextUtils.reuseConnection();
            if (Objects.nonNull(connection)) {
                ThreadReuseContextUtils.useIncrease();
                return connection;
            } else if (ThreadReuseContextUtils.hasConnection()) {
                return delegate.getConnection(username, password);
            } else {
                connection = delegate.getConnection(username, password);
                connection = new ThreadReuseConnection(connection);
                ThreadReuseContextUtils.setThreadReuseContext(connection);
                return connection;
            }
        } else {
            return delegate.getConnection();
        }
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return delegate.unwrap(iface);
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return delegate.isWrapperFor(iface);
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return delegate.getLogWriter();
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {
        delegate.setLogWriter(out);
    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {
        delegate.setLoginTimeout(seconds);
    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return delegate.getLoginTimeout();
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return delegate.getParentLogger();
    }
}
